//
//  PhotoUploadViewController.m
//  MySpaceID.Demo
//
//  Created by Haider Sabri on Thursday10/15/09.
//  Copyright 2009 MySpace, Inc. All rights reserved.
//

#import "PhotoUploadViewController.h"
#import "XPathQuery.h"


@implementation PhotoUploadViewController
@synthesize context;
@synthesize restApi;
@synthesize roaApi;
@synthesize userId;
@synthesize albumId;
@synthesize imagePickerController;

- (void) testPhotoUpload{
	
	NSData *albumsData = [restApi getAlbums:userId];
	NSArray *result = PerformXMLXPathQuery2(albumsData, @"//ms:user/ms:albums/ms:album/ms:albumid", @"ms", @"api-v1.myspace.com");
	NSDictionary *node = [result objectAtIndex:0];
	NSString *value = [node objectForKey:@"nodeContent"];
	albumId = [value integerValue];
	UIImage *image = [UIImage imageNamed:@"doubleicon.png"];
	NSData *photoData = UIImagePNGRepresentation(image);
	roaApi = [RoaApi apiWithContext:context];
	[roaApi addPhoto:userId albumId:albumId caption:@"iPhone" photoData:[photoData retain] imageType:@"image/png"];
	
	
	
}


- (void) viewDidLoad{

	if(context == nil)
	{
		context = [OffsiteContext contextWithConsumerKey:@"18bed0f4247a4f79bc9941bfed5b534c"  
										  consumerSecret:@"2e8ebdafbef844a5929086e659e4188c" tokenKey:nil tokenSecret:nil] ;
		[context setUrlScheme:@"myspaceid2"];
		[context retain];
	}
	bool loggedIn = [context isLoggedIn];
	if(loggedIn)
	{
		
		restApi = [RestV1 apiWithContext: context] ;
		[restApi retain];
		
		if(userId == nil)
		{
			NSData *userData = [restApi getCurrentUser];
			NSArray *result = PerformXMLXPathQuery2(userData, @"//ms:user/ms:userid", @"ms", @"api-v1.myspace.com");
			NSDictionary *node = [result objectAtIndex:0];
			NSString *value = [node objectForKey:@"nodeContent"];
			if(value)
			{
				userId = [value integerValue];
			}
		}
		
		
	}
	if(userId)
	{
		//[self testPhotoUpload];
		[self getPhotos];
	}
}

- (void) getPhotos {

	NSData *albumsData = [restApi getAlbums:userId];
	NSArray *result = PerformXMLXPathQuery2(albumsData, @"//ms:user/ms:albums/ms:album/ms:albumid", @"ms", @"api-v1.myspace.com");
	NSDictionary *node = [result objectAtIndex:0];
	NSString *value = [node objectForKey:@"nodeContent"];
	if(value)
	{
		albumId = [value integerValue];
	}
	
	

	
}
- (void)dealloc {
	[context release];
	[restApi release];
	[roaApi release];
	[imagePickerController release];
    [super dealloc];
}

- (IBAction) uploadPhoto : (id) sender{
	
	if(albumId)
	{
	
		imagePickerController = [[UIImagePickerController alloc] init];
		imagePickerController.delegate = self;
		imagePickerController.sourceType = 
		UIImagePickerControllerSourceTypePhotoLibrary;
		[self presentModalViewController:imagePickerController animated:YES];
		
		
	}
	
}


#pragma mark --
#pragma mark UIImagePickerController Methods

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
				  editingInfo:(NSDictionary *)editingInfo{
	
	[picker dismissModalViewControllerAnimated:YES];
    imagePickerController.view.hidden = YES;
	NSData *photoData = UIImageJPEGRepresentation(image, 1.0f);
	roaApi = [RoaApi apiWithContext:context];
	[roaApi addPhoto:userId albumId:albumId caption:@"iPhone" photoData:photoData imageType:@"image/jpg"];
	//[roaApi addPhoto:userId albumId:albumId caption:@"iphone" photo:image];
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
	
}
@end
